package com.shenma2009.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;

/**
 * @author 军哥
 * @version 1.0
 * @description: WeixinPayUtils
 * @date 2023/4/28 14:34
 */


//需要添加的依赖
//<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
//<dependency>
//<groupId>org.apache.httpcomponents</groupId>
//<artifactId>httpcore</artifactId>
//<version>4.4.10</version>
//</dependency>
//
//<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
//<dependency>
//<groupId>org.apache.httpcomponents</groupId>
//<artifactId>httpclient</artifactId>
//<version>4.5.6</version>
//</dependency>
//
//<dependency>
//<groupId>com.alibaba.fastjson2</groupId>
//<artifactId>fastjson2</artifactId>
//<version>2.0.20</version>
//</dependency>

public class WeixinPayUtils {

    public static String GET_ORDER_URL = "https://www.shenmazong.com/weixin/getOrderNo";
    public static String GET_ORDER_STATE_URL = "https://www.shenmazong.com/weixin/getOrderInfo?orderNo=";
    public static String POST_ORDER_PAY_URL = "https://www.shenmazong.com/weixin/postOrderObj";

    /**
     * @description 获取订单编号
     * @author 军哥
     * @date 2023/4/28 14:55
     * @version 1.0
     */
    public String getOrderNo() throws URISyntaxException, IOException {

        CloseableHttpClient client = HttpClients.createDefault();
        URIBuilder uriBuilder = new URIBuilder(GET_ORDER_URL);

        HttpGet httpGet = new HttpGet(uriBuilder.build());

        // httpGet.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
        CloseableHttpResponse response = client.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        HttpEntity entity = response.getEntity();

        String str = EntityUtils.toString(entity);
        System.out.println(str);
        response.close();
        client.close();

        // 处理字符串返回值
        JSONObject jsonObject = JSON.parseObject(str);
        String orderNo = jsonObject.getString("data");

        return orderNo;
    }

    /**
     * @description 获取订单状态
     * @author 军哥
     * @date 2023/4/28 14:56
     * @version 1.0
     */
    public Integer getOrderState(String orderNo) throws URISyntaxException, IOException {

        CloseableHttpClient client = HttpClients.createDefault();
        URIBuilder uriBuilder = new URIBuilder(GET_ORDER_STATE_URL + orderNo);

        HttpGet httpGet = new HttpGet(uriBuilder.build());

        // httpGet.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
        CloseableHttpResponse response = client.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        HttpEntity entity = response.getEntity();

        String str = EntityUtils.toString(entity);
        System.out.println(str);
        response.close();
        client.close();

        // 处理字符串返回值
        JSONObject jsonObject = JSON.parseObject(str);
        Integer code = jsonObject.getInteger("code");

        if(code == 0) {
            JSONObject data = jsonObject.getJSONObject("data");
            Integer orderStatus = data.getInteger("orderStatus");

            return orderStatus;
        }

        return 500;
    }

    /**
     * @description 支付接口
     * @author 军哥
     * @date 2023/4/28 15:06
     * @version 1.0
     */
    public String postOrderPay(String orderId, Integer amount, String desc) {
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // 创建httpPost远程连接实例
        HttpPost post = new HttpPost(POST_ORDER_PAY_URL);
        String result = "";
        try (CloseableHttpClient closeableHttpClient = httpClientBuilder.build()) {
            // HttpEntity entity = new StringEntity(jsonDataStr);
            // 修复 POST json 导致中文乱码

            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put("outTradeNo", orderId);
            paramMap.put("description", desc);
            paramMap.put("amount", amount);
            paramMap.put("attach", "test");

            HttpEntity entity = new StringEntity(JSON.toJSONString(paramMap), "UTF-8");
            post.setEntity(entity);
            post.setHeader("Content-type", "application/json");
            HttpResponse resp = closeableHttpClient.execute(post);
            try {

                if(resp.getStatusLine().getStatusCode() == 200) {
                    HttpEntity respEntity = resp.getEntity();
                    String str = EntityUtils.toString(respEntity);
                    result = str;

                    // 解析返回接口
                    JSONObject jsonObject = JSON.parseObject(str);
                    JSONObject data = jsonObject.getJSONObject("data");
                    String codeUrl = data.getString("codeUrl");
                    result = codeUrl;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }
}
